var gulp = require('gulp'),
	gulpServer = require('gulp-live-server'),
	gulpWatch = require('gulp-watch'),
	plumber = require('gulp-plumber'),
	gulpSass = require('gulp-sass'),
	concat = require('gulp-concat'),
	clean = require('gulp-clean'),
	notify = require('gulp-notify'),
	sftp = require('gulp-sftp'),
	webServer = require('gulp-webserver'),
	gulpUglify = require('gulp-uglify');


//路径定义
var path = {
	source: './static',
	dist: './dist'
};

//项目打包
var buildProject = {
	/* step1 清除发布目录 */
	cleanDest: function(){
		return gulp.src(path.dist).pipe(clean());
	},
	/* step2 图片资源打版本号 */
	revImg: function(){
		return gulp.src([path.source + '/**/*.png', path.source + '/**/*.jpg', path.source + '/**/*.gif'])
				// .pipe(gulp.dest(path.dist))
				.pipe(rev())
				.pipe(gulp.dest(path.dist))
				.pipe(rev.manifest({
					path: 'manifest-assets.json'
				}))
				.pipe(gulp.dest(path.dist));
	},
	/* step3 css资源打版本号 */
	revStylesheets: function(){
		return gulp.src(path.source + '/**/*.css')
				// .pipe(gulp.dest(path.dist))
				.pipe(rev())
				.pipe(gulp.dest(path.dist))
				.pipe(rev.manifest({
					path: 'manifest-stylesheets.json'
				}))
				.pipe(gulp.dest(path.dist));
	},
	/* step4 js资源打版本号 */
	revScripts: function(){
		return gulp.src([path.source + '/**/*.js'])
			// .pipe(gulp.dest(path.dist))
			.pipe(rev())
			.pipe(gulp.dest(path.dist))
			.pipe(rev.manifest({
				path: 'manifest-script.json'
			}))
			.pipe(gulp.dest(path.dist));
	},
	uglifyJs: function(){
		gulp.src(['./dist/scripts/common/*.js'])
			.pipe(gulpUglify({
				mangle: false
			}).on('error', function(e){
				console.log(e)
			}))
			.pipe(gulp.dest('./dist/scripts/common'));
		gulp.src(['./dist/scripts/apps/**/*.js'])
			.pipe(gulpUglify({
				mangle: false
			}).on('error', function(e){
				console.log(e)
			}))
			.pipe(gulp.dest('./dist/scripts/apps'));
	}
};
//项目开发
var devProject = {
	/* 本地server建立 */
	createServer: function(){
		var server = gulpServer.static('/', 8089);
		server.start();
	},
	/* sass编译 */
	compileSass: function(){
		return gulp.src([path.source + '/sass/**/*.scss'])
			.pipe(plumber({
				errorHandler: notify.onError('Error: <%= error.message %>')
			}))
			.pipe(gulpSass())
			.pipe(gulp.dest(path.source + '/stylesheets'));
	},
	/* 监听sass文件变动 */
	watchSass: function(){
		gulp.watch('./static/sass/**/*.scss', ['compileSass']);
	},
	webServer: function(){
		gulp.src('./')
			.pipe(webServer({
				livereload: true,
				host: 'localhost'
			}));
	}
};

gulp.task('createServer', devProject.createServer);
gulp.task('compileSass', devProject.compileSass);
gulp.task('watchSass', devProject.watchSass);
gulp.task('webServer', devProject.webServer);


gulp.task('default', ['watchSass']);



/*
	task注册
*/
gulp.task('cleanDest', buildProject.cleanDest);
gulp.task('revImg', buildProject.revImg);
gulp.task('revStylesheets', buildProject.revStylesheets);
gulp.task('revScripts', buildProject.revScripts);
gulp.task('revControlImg', buildProject.revControlImg);
gulp.task('revControlScript', buildProject.revControlScript);
gulp.task('revControlJsp', buildProject.revControlJsp);
gulp.task('revControlHtml', buildProject.revControlHtml);
gulp.task('uglifyJs', buildProject.uglifyJs);
